package com.store.orm.io;

import com.store.orm.config.MongoConfig;
import com.store.orm.config.MySQLConfig;
import com.store.orm.config.Pool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * Properties 配置文件解析类
 * 主要是将 properties 配置文件解析为 实体类，方便调用
 *
 * @author 宫静雨
 * @version 1.0
 */
public class ParseProperties {
    private static final Logger logger = LoggerFactory.getLogger(ParseProperties.class);

    public MongoConfig parseMongo(File file) {
        MongoConfig config = new MongoConfig();
        Properties properties;
        try {
            FileInputStream fis = new FileInputStream(file);
            properties = new Properties();
            properties.load(fis);
        } catch (IOException e) {
            logger.error("解析Properties配置文件时发生错误: {}", e.getMessage());
            throw new RuntimeException();
        }
        config.setHost(properties.getProperty("host"));
        config.setPort(Integer.valueOf(properties.getProperty("port")));
        config.setDb(properties.getProperty("db"));
        Pool pool = new Pool();
        if (properties.getProperty("pool.max") != null) {
            pool.setMax(Integer.valueOf(properties.getProperty("pool.max")));
        }
        if (properties.getProperty("pool.init") != null) {
            pool.setInit(Integer.valueOf(properties.getProperty("pool.init")));
        }
        config.setPool(pool);
        return config;
    }

    public MySQLConfig parseMySQL(File file) {
        MySQLConfig config = new MySQLConfig();
        Properties properties;
        try {
            FileInputStream fis = new FileInputStream(file);
            properties = new Properties();
            properties.load(fis);
        } catch (IOException e) {
            logger.error("解析Properties配置文件时发生错误: {}", e.getMessage());
            throw new RuntimeException();
        }
        config.setDriver(properties.getProperty("driver"));
        config.setUrl(properties.getProperty("url"));
        config.setUsername(properties.getProperty("username"));
        config.setPassword(properties.getProperty("password"));
        config.setMaxBatch(Integer.parseInt(properties.getProperty("maxBatch")));
        config.setLogged(Boolean.parseBoolean(properties.getProperty("logged")));
        config.setCamelCase(Boolean.parseBoolean(properties.getProperty("camelCase")));
        config.setBatchLog(Boolean.parseBoolean(properties.getProperty("batchLog")));
        config.setTruncate(Boolean.parseBoolean(properties.getProperty("truncate")));
        config.setUpdateAll(Boolean.parseBoolean(properties.getProperty("updateAll")));
        Pool pool = new Pool();
        if (properties.getProperty("pool.max") != null) {
            pool.setMax(Integer.valueOf(properties.getProperty("pool.max")));
        }
        if (properties.getProperty("pool.init") != null) {
            pool.setInit(Integer.valueOf(properties.getProperty("pool.init")));
        }
        config.setPool(pool);
        return config;
    }
}